// 使用 AES 加密算法
class CryptoUtil {
    constructor(secretKey) {
        this.secretKey = secretKey;
    }

    // 加密数据
    async encrypt(data) {
        const encoder = new TextEncoder();
        const encodedData = encoder.encode(JSON.stringify(data));
        
        // 从密钥生成加密密钥
        const keyMaterial = await crypto.subtle.importKey(
            "raw",
            encoder.encode(this.secretKey),
            { name: "PBKDF2" },
            false,
            ["deriveBits", "deriveKey"]
        );

        // 生成随机 IV
        const iv = crypto.getRandomValues(new Uint8Array(12));
        
        // 派生实际的加密密钥
        const key = await crypto.subtle.deriveKey(
            {
                name: "PBKDF2",
                salt: new Uint8Array(16),
                iterations: 100000,
                hash: "SHA-256"
            },
            keyMaterial,
            { name: "AES-GCM", length: 256 },
            true,
            ["encrypt"]
        );

        // 加密数据
        const encryptedContent = await crypto.subtle.encrypt(
            {
                name: "AES-GCM",
                iv: iv
            },
            key,
            encodedData
        );

        // 组合 IV 和加密数据
        const encryptedArray = new Uint8Array(iv.length + encryptedContent.byteLength);
        encryptedArray.set(iv);
        encryptedArray.set(new Uint8Array(encryptedContent), iv.length);

        // 转换为 Base64 字符串
        return btoa(String.fromCharCode.apply(null, encryptedArray));
    }

    // 解密数据
    async decrypt(encryptedData) {
        try {
            // 解码 Base64
            const encryptedArray = new Uint8Array(atob(encryptedData).split('').map(char => char.charCodeAt(0)));
            
            // 提取 IV
            const iv = encryptedArray.slice(0, 12);
            const data = encryptedArray.slice(12);

            const encoder = new TextEncoder();
            
            // 从密钥生成加密密钥
            const keyMaterial = await crypto.subtle.importKey(
                "raw",
                encoder.encode(this.secretKey),
                { name: "PBKDF2" },
                false,
                ["deriveBits", "deriveKey"]
            );

            // 派生实际的解密密钥
            const key = await crypto.subtle.deriveKey(
                {
                    name: "PBKDF2",
                    salt: new Uint8Array(16),
                    iterations: 100000,
                    hash: "SHA-256"
                },
                keyMaterial,
                { name: "AES-GCM", length: 256 },
                true,
                ["decrypt"]
            );

            // 解密数据
            const decryptedContent = await crypto.subtle.decrypt(
                {
                    name: "AES-GCM",
                    iv: iv
                },
                key,
                data
            );

            // 转换回原始数据
            const decoder = new TextDecoder();
            return JSON.parse(decoder.decode(decryptedContent));
        } catch (error) {
            console.error('Decryption failed:', error);
            throw new Error('Decryption failed');
        }
    }
}

export default CryptoUtil; 